package cn.agiledata.bank.common.action;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.agiledata.bank.common.bean.SelectPeerAccountBean;
import cn.agiledata.bank.common.dao.AccountTemplateInter;
import cn.agiledata.bank.common.util.Constant;
import cn.agiledata.bank.common.util.PageBean;
import cn.agiledata.bank.common.util.StringUtil;
import cn.agiledata.bank.common.util.WeatherUtil;
import cn.agiledata.bank.hibernateBean.PeerInnerAccount;
import cn.agiledata.bank.myspace.bean.PeerBankAccountQueryBean;
import cn.agiledata.bank.myspace.bean.PeerInnerAccountQueryBean;
import cn.agiledata.bank.myspace.service.PeerAccountService;
import com.vnv.infrastructure.action.IPBBaseAction;
import com.vnv.infrastructure.exception.IPBException;
import com.vnv.infrastructure.logic.IPBAppFactory;
import com.vnv.infrastructure.logic.IPBMgrFactory;

public class SelectAction extends IPBBaseAction {
	
	/**
	 * 得到交易服务
	 * @return
	 * @throws IPBException
	 */
	protected AccountTemplateInter getAccountMgr() throws IPBException {
		return (AccountTemplateInter)IPBMgrFactory.getMgr("accountTemplate");
	}

	/**
	 * 选择email
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IPBException
	 * @throws Exception
	 */
	public ActionForward selectEmail(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IPBException, Exception {
		PeerAccountService service = getPeerAccountService();
		
		PeerInnerAccountQueryBean bean = new PeerInnerAccountQueryBean();
		
		bean.setAccountId(new Long(WeatherUtil.getAccountIdFromSession(request)));
		
		String startPage = request.getParameter("startPage");
        if (StringUtils.isEmpty(startPage)) {
            startPage = "1"; // 默认显示第1页
        }
        bean.setStartPage(startPage);
        
        String pageRecord = request.getParameter("pageRecord");
        if (StringUtils.isEmpty(pageRecord)) {
            pageRecord = "10"; // 默认每页显示10条
        }
		bean.setPageRecord(pageRecord);
		
		PageBean pb = service.getPeerInnerAccountList(bean);
		List list = pb.getData();
		SelectPeerAccountBean selectBean = null;
		List newList = new ArrayList();
		if(list != null) {
			Iterator iterator = (Iterator)list.iterator();
			while(iterator.hasNext()) {
				PeerInnerAccount pia = (PeerInnerAccount)iterator.next();
				
				String email = pia.getPeerAccount().getEmail();
				String mobile = pia.getPeerAccount().getMobile();
				
				//对email状态不正常或mobile状态不正常的账户进行处理
				if (!Constant.ACCOUNT_STATE_NORMAL.equals(pia.getPeerAccount().getEmStatus())) {//如果预设账户的email状态不正常，则令其email为null
					if (!StringUtil.isStrEmpty(email)) {
						email = null;
					}
				} else if (!Constant.ACCOUNT_STATE_NORMAL.equals(pia.getPeerAccount().getMobStatus())) {//如果预设账户的mobile状态不正常，则令其mobile为null
					if (!StringUtil.isStrEmpty(mobile)) {
						mobile = null;
					}
				}
				
				String value = email;
				if(StringUtil.isStrEmpty(email)) {
					value = mobile;
				}
				
				selectBean = new SelectPeerAccountBean(value,email,mobile,pia.getPeerAccount().getName(),pia.getSource());
				newList.add(selectBean);
			}
		}
		pb.setData(newList);
		
		request.setAttribute("list", pb);
		return mapping.findForward("email");
		
		/*
		//得到账号id
		long accountId = WeatherUtil.getAccountIdFromSession(request);
		List list = getAccountMgr().getPeerAccountListById(String.valueOf(accountId));
		SelectPeerAccountBean selectBean = null;
		List newList = new ArrayList();
		if(list != null) {
			Iterator iterator = (Iterator)list.iterator();
			while(iterator.hasNext()) {
				Object[] objects = (Object[])iterator.next();
				String email = (String)objects[0];
				String mobile = (String)objects[1];
				
				String value = email;
				if(StringUtil.isStrEmpty(email)) {
					value = mobile;
				}
				selectBean = new SelectPeerAccountBean(value,email,mobile,(String)objects[2],(String)objects[3]);
				newList.add(selectBean);
			}
		}
		request.setAttribute("list",newList);
		return mapping.findForward("email");*/
	}
	
	/**
	 * 得到PeerAccountService
	 * @return
	 */
	protected PeerAccountService getPeerAccountService() {
		return (PeerAccountService)IPBAppFactory.getObj("peerAccountService");
	}
	
	/**
	 * 选择银行账户
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws IPBException
	 * @throws Exception
	 */
	public ActionForward selectBankAccount(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IPBException, Exception {
		PeerAccountService service = getPeerAccountService();
		
		PeerBankAccountQueryBean bean = new PeerBankAccountQueryBean();
		
		bean.setAccountId(new Long(WeatherUtil.getAccountIdFromSession(request)));
		
		String startPage = request.getParameter("startPage");
        if (StringUtils.isEmpty(startPage)) {
            startPage = "1"; // 默认显示第1页
        }
        bean.setStartPage(startPage);
        
        String pageRecord = request.getParameter("pageRecord");
        if (StringUtils.isEmpty(pageRecord)) {
            pageRecord = "10"; // 默认每页显示10条
        }
		bean.setPageRecord(pageRecord);
		
		PageBean pb = service.getPeerBankAccountList(bean);
		
		request.setAttribute("list", pb);
		return mapping.findForward("success");
	}
}
